var gobang = document.getElementById('goBang');
var context = gobang.getContext('2d');
var board = [];
var over = false;
var isMe = true;

//赢法数组
var wins = [];
for(var i=0;i<15;i++){
	wins[i] = [];
	for(var j=0;j<15;j++){
		wins[i][j] = [];
	}
}

//赢法种类索引值
var count = 0;

//赢法计算
//所有横排赢法
for(var i=0;i<15;i++){
	for(var j=0;j<11;j++){
		for(var k=0;k<5;k++){
			wins[i][j+k][count] = true;
		}
		count++;
	}
}
//所有竖排赢法
for(var i=0;i<15;i++){
	for(var j=0;j<11;j++){
		for(var k=0;k<5;k++){
			wins[j+k][i][count] = true;	
		}
		count++;
	}
}
//所有斜线赢法
for(var i=0;i<11;i++){
	for(var j=0;j<11;j++){
		for(var k=0;k<5;k++){
			wins[i+k][j+k][count] = true;	
		}
		count++;
	}
}
//所有反斜线赢法
for(var i=0;i<11;i++){
	for(var j=14;j>3;j--){
		for(var k=0;k<5;k++){
			wins[i+k][j-k][count] = true;	
		}
		count++;
	}
}

//赢法统计数组
var myWin = [];
var computerWin = [];

for(var i=0;i<count;i++){
	myWin[i] = 0;
	computerWin[i] = 0;
}


//棋牌落子状态
for(var i=0; i<15;i++){
	board[i] = [];
	for(var j=0; j<15; j++){
		board[i][j] = 0;
	}
}
context.strokeStyle = "#ddd";
var logo = new Image();
logo.src = "img/2code.jpg";
logo.onload = function() {
	context.drawImage(logo, 140,140,170,170);
	drawBoard();
}
//绘制棋盘
function drawBoard() {
	for(var i = 0; i < 15; i++) {
		context.moveTo(15, 15 + i * 30);
		context.lineTo(435, 15 + i * 30);
		context.stroke();
		context.moveTo(15 + i * 30, 15);
		context.lineTo(15 + i * 30, 435);
		context.stroke();
	}
}

//绘制棋子
function  oneStep(i,j,isMe){
	context.beginPath(); 
	context.arc(15 + i * 30,15 + j * 30,13,0,2*Math.PI);
	var gradient = context.createRadialGradient(15 + i * 30+2,15 + j * 30-2,13,15 + i * 30+2,15 + j * 30-2,0);
	if(isMe){
		gradient.addColorStop(0,'#0a0a0a');
		gradient.addColorStop(1,'#636363');
	}else{
		gradient.addColorStop(0,'#d1d1d1');
		gradient.addColorStop(1,'#f9f9f9');
	}
	context.fillStyle = gradient;
	context.fill();
	context.closePath();
}
//落子
gobang.onclick = function(e){
	if(over){
		return;
	}
	if(!isMe){
		return;
	}
	var x = e.offsetX;
	var y = e.offsetY;
	var i = Math.floor(x/30);
	var j = Math.floor(y/30);
	if(board[i][j] == 0){
		oneStep(i,j,isMe);
		board[i][j] = 1;
		for(var k=0;k<count;k++){
			if(wins[i][j][k]){
				myWin[k]++;
				computerWin[k] = 6;//标记当前位置被占不可能赢
				if(myWin[k] == 5){
					window.alert("恭喜，你赢了！");
					over = true;
				}
			}
		}
		if(!over){
			isMe = !isMe;
			computerAI();
		}
	}
}

function computerAI(){
	var myScore = [];
	var computerScore = [];
	var max = 0//保存最高分数的位置(最有利位置)
	var p = 0 , q = 0;//最高分数位置坐标
	for(var i=0;i<15;i++){
		myScore[i]=[];
		computerScore[i]=[];
		for(var j=0;j<15;j++){
			myScore[i][j] = 0;
			computerScore[i][j] = 0;
		}
	}
	for(var i=0;i<15;i++){
		for(var j=0;j<15;j++){
			if(board[i][j] == 0){
				for(var k=0;k<count;k++){
					if(wins[i][j][k]){
						if(myWin[k] == 1){
							myScore[i][j] += 200;
						}else if(myWin[k] == 2){
							myScore[i][j] += 500;
						}else if(myWin[k] == 3){
							myScore[i][j] += 2000;
						}else if(myWin[k] == 4){
							myScore[i][j] += 10000;
						}
						if(computerWin[k] == 1){
							computerScore[i][j] += 400;
						}else if(computerWin[k] == 2){
							computerScore[i][j] += 800;
						}else if(computerWin[k] == 3){
							computerScore[i][j] += 4000;
						}else if(computerWin[k] == 4){
							computerScore[i][j] += 20000;
						}
					}
				}
				if(myScore[i][j] > max){
					max = myScore[i][j];
					p = i;
					q = j;
				}else if(myScore[i][j] == max){
					if(computerScore[i][j] > computerScore[p][q]){
						p = i;
						q = j;
					}
				}
				if(computerScore[i][j] > max){
					max = computerScore[i][j];
					p = i;
					q = j;
				}else if(computerScore[i][j] == max){
					if(myScore[i][j] > myScore[p][q]){
						p = i;
						q = j;
					}
				}
			}
		}
	}
	oneStep(p,q,false);
	board[p][q] = 2;
	for(var k=0;k<count;k++){
		if(wins[p][q][k]){
			computerWin[k]++;
			myWin[k] = 6;//标记当前位置被占不可能赢
			if(computerWin[k] == 5){
				window.alert("好可惜，你输了！");
				over = true;
			}
		}
	}
	if(!over){
		isMe = !isMe;
	}
}
